package com.lagou.work.task02.classloader;

import com.lagou.work.task03.angel.utils.IOUtil;

import java.io.*;
import java.net.Socket;

/**
 * tcp通信客户端
 */

public class ClientTest {

    public static void main(String[] args) throws InterruptedException {
        Socket socket = null;
        try {
            String path = "D://BubbleSort.class";
            socket = new Socket("localhost",8885);
            if ( socket == null) {
                System.out.println("客户端发送失败!!");
                return;
            }
            sendByteCodeFile2Server(path, socket);
            System.out.println("客户端发送成功!!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void sendByteCodeFile2Server(String path, Socket socket) {
        FileInputStream fis = null;
        BufferedOutputStream bos = null;
        OutputStream outputStream = null;

        try {
            fis = new FileInputStream(path);
            outputStream = socket.getOutputStream();
            bos = new BufferedOutputStream(outputStream);
            byte[] buffer = new byte[1024];
            int pos;
            while ((pos = fis.read(buffer))!=-1){
                bos.write(buffer,0,pos);
            }

            bos.flush();
        } catch (IOException e) {
                e.printStackTrace();
        }finally {
            IOUtil.close(socket, fis, bos, outputStream,null);
        }

    }



}
